package com.ruoyi.project.system.service.impl;

import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysCodeMapper;
import com.ruoyi.project.system.domain.SysCode;
import com.ruoyi.project.system.service.ISysCodeService;

/**
 * 巡查码Service业务层处理
 * 
 * @author jjp
 * @date 2020-03-30
 */
@Service
public class SysCodeServiceImpl implements ISysCodeService 
{
    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private ISysCodeService sysCodeService;

    /**
     * 查询巡查码
     * 
     * @param codeId 巡查码ID
     * @return 巡查码
     */
    @Override
    public SysCode selectSysCodeById(Long codeId)
    {
        return sysCodeMapper.selectSysCodeById(codeId);
    }

    /**
     * 查询巡查码列表
     * 
     * @param sysCode 巡查码
     * @return 巡查码
     */
    @Override
    public List<SysCode> selectSysCodeList(SysCode sysCode)
    {
        return sysCodeMapper.selectSysCodeList(sysCode);
    }

    /**
     * 新增巡查码
     * 
     * @param sysCode 巡查码
     * @return 结果
     */
    @Override
    public int insertSysCode(SysCode sysCode)
    {
        sysCode.setCreateTime(DateUtils.getNowDate());
        return sysCodeMapper.insertSysCode(sysCode);
    }

    /**
     * 修改巡查码
     * 
     * @param sysCode 巡查码
     * @return 结果
     */
    @Override
    public int updateSysCode(SysCode sysCode)
    {
        return sysCodeMapper.updateSysCode(sysCode);
    }

    /**
     * 批量删除巡查码
     * 
     * @param codeIds 需要删除的巡查码ID
     * @return 结果
     */
    @Override
    public int deleteSysCodeByIds(Long[] codeIds)
    {
        return sysCodeMapper.deleteSysCodeByIds(codeIds);
    }

    /**
     * 删除巡查码信息
     * 
     * @param codeId 巡查码ID
     * @return 结果
     */
    @Override
    public int deleteSysCodeById(Long codeId)
    {
        return sysCodeMapper.deleteSysCodeById(codeId);
    }

    @Override
    public Long createCodeId() {
        return returnCard();
    }
    public Long num(){
        Random rand = new Random();
        String num = "";
        for (int a = 0; a < 8; a++) {
            //生成8位数字
            num += rand.nextInt(10);
        }
        long codeId =Long.parseLong(num);
        return codeId;
    }
    public Long returnCard(){
        Long codeId=num();
        SysCode code = sysCodeService.selectSysCodeById(codeId);
        if(code != null){
            return returnCard();
        }else{
            return codeId;
        }
    }

}
